package com.common.UITags;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.bean.SelectItem;
import com.common.GlobalPara;
import com.jfinal.kit.JsonKit;

/**
 * datagrid的标题
 * @author zhanglong
 *
 */
public class ColumnTextTag extends TagSupport{
	private static final long serialVersionUID = 1L;
	
	private String filed = "";
	private String text = "";
	private String sortable;//是否可以排序
	private String width;//宽度
	private String hidden = "false";
	private String checkbox = "false";
	private String code;// 代码值
	private String data;//一些固定的数据，比如编辑 修改等超链接
	
	JspWriter out;
	StringBuffer sBF = new StringBuffer();
	
	@Override
	public int doStartTag() throws JspException {
		try{
			List list;
			Map<String,String> codeMap;
			List<SelectItem> codeList = new ArrayList<SelectItem>();
			String dropdownData;
			
			Map map = new HashMap<String, String>();
			out = pageContext.getOut();
			sBF.setLength(0);
			sBF.append(" <th data-options=\"");
			sBF.append("field:'"+ filed +"',");
			sBF.append("align:'" +"center'");
			if(!(sortable==null||"".equals(sortable))){
				sBF.append(",sortable:'" +sortable+"'");
			}
			if(!(width==null||"".equals(width))){
				sBF.append(",width:" +width);
			}
			if(!(hidden == null ||"".equals(hidden))){
				sBF.append(",hidden:" + hidden);
			}
			if(!(checkbox == null ||"".equals(checkbox))){
				sBF.append(",checkbox:" + checkbox);
			}
			if (!(code == null || "".equals(code))&&!(filed==null || "".equals(filed))) {
				codeMap = GlobalPara._codeMap.get(code.toUpperCase());
				//当前台写的code为合法的的时候才进行转换，否则直接展示后台数据
				if(codeMap!=null){
					codeList = GlobalPara._codelist.get(code.toUpperCase());
					dropdownData = JsonKit.toJson(codeList);
					dropdownData = dropdownData.replaceAll("\"", "'");
					String codeJson = JsonKit.toJson(codeMap);
					codeJson = codeJson.replaceAll("\"", "\\\\'");
					sBF.append(",formatter:function(value,row){");
					sBF.append("var codejson ='" + codeJson + "';");
					sBF.append("var obj =  eval(\'(\'+codejson+\')\');");
					sBF.append("return obj[value];");
					sBF.append("}");
					sBF.append(",editor:{");
					sBF.append("type:'combobox',");
					sBF.append("options:{");
					sBF.append("valueField:'value',");
					sBF.append("textField:'text',");
					sBF.append("data:" + dropdownData + ",");
					sBF.append("}");
					sBF.append("}");
				}
			}else if(!(data==null || "".equals(data))){
				sBF.append(",formatter:function(value,row){");
				sBF.append("var returnvalue = \'" +data+"\';");
				sBF.append(" return returnvalue;");
				sBF.append("}");
			}
			
			sBF.append("\"");
			
			sBF.append(">");
			sBF.append(text);
			sBF.append("</th>");
			
			//不是隐藏列才会被导出
			if("false".equals(hidden)){
				//数据导出
				list = ((DataGridTag)getParent()).getHeadList();
				map.put(filed, text);
				list.add(map);
				//数据导出
			}
			
			out.print(sBF.toString());
		}catch (IOException e) {
			throw new JspException("标签往前台写数据时异常");
		}catch(Exception e){
			throw new JspException("其他异常");
		}
		
		return SKIP_BODY;
	}
	
	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	//get and set方法

	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}

	public String getSortable() {
		return sortable;
	}

	public void setSortable(String sortable) {
		this.sortable = sortable;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getHidden() {
		return hidden;
	}

	public void setHidden(String hidden) {
		this.hidden = hidden;
	}

	public String getCheckbox() {
		return checkbox;
	}

	public void setCheckbox(String checkbox) {
		this.checkbox = checkbox;
	}

	public String getFiled() {
		return filed;
	}

	public void setFiled(String filed) {
		this.filed = filed;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}



}
